package com.wuze.cloud.util;

import com.wuze.cloud.enums.SysFileTypeEnum;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: wuze
 * @description:
 * @date: 2023/03/14
 */
public class FileUtil {

    private static List<String> DOCUMENT_FILE = new ArrayList<>();
    private static List<String> IMAGE_FILE = new ArrayList<>();
    private static List<String> VIDEO_FILE = new ArrayList<>();
    private static List<String> AUDIO_FILE = new ArrayList<>();

    static {
        DOCUMENT_FILE.add(".docx");
        DOCUMENT_FILE.add(".txt");
        DOCUMENT_FILE.add(".doc");
        DOCUMENT_FILE.add(".xls");
        DOCUMENT_FILE.add(".xlsx");
        DOCUMENT_FILE.add(".ppt");
        DOCUMENT_FILE.add(".pptx");
        DOCUMENT_FILE.add(".pdf");
        DOCUMENT_FILE.add(".html");
        //BMP(位图)、JPG、JPEG、PNG、GIF
        IMAGE_FILE.add(".bmp");
        IMAGE_FILE.add(".jpg");
        IMAGE_FILE.add(".jpeg");
        IMAGE_FILE.add(".png");
        IMAGE_FILE.add(".gif");

        //AVI、mov、rmvb、rm、FLV、mp4、3GP
        VIDEO_FILE.add(".avi");
        VIDEO_FILE.add(".mov");
        VIDEO_FILE.add(".rmvb");
        VIDEO_FILE.add(".rm");
        VIDEO_FILE.add(".flv");
        VIDEO_FILE.add(".mp4");
        VIDEO_FILE.add(".3gp");

    }

    public static String getFileType(String suffix){
        //转为小写
        suffix = suffix.toLowerCase();
        if(DOCUMENT_FILE.contains(suffix)){
            return SysFileTypeEnum.DOCUMENT_FILE.name();
        }
        if(IMAGE_FILE.contains(suffix)){
            return SysFileTypeEnum.IMAGE_FILE.name();
        }
        if(VIDEO_FILE.contains(suffix)){
            return SysFileTypeEnum.VIDEO_FILE.name();
        }
        return SysFileTypeEnum.OTHER_FILE.name();
    }


    /**
     * File转byte[]数组
     *
     * @param file
     * @return
     */
    public static byte[] file2byte(File file) {
        if (file == null) {
            return null;
        }
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fileInputStream.read(b)) != -1) {
                byteArrayOutputStream.write(b, 0 , n);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
